/**
 * Created by Administrator on 2018/4/18.
 */
import api from './index'
import checksign from './checksign'


function queryJsConfig(param, callback) {
  api.post(checksign.apendSign('/wechat/jsconfig'), param, r => {
    callback(null, r)
  }, f => {
    callback(f)
  })
}

function GetQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}

function  GetHashQueryString(name){
  var after = window.location.hash.split("?")[1];
  if(after)
  {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = after.match(reg);
    if(r != null)
    {
      return  decodeURIComponent(r[2]);
    }
    else
    {
      return null;
    }
  }
}

export default {
  queryJsConfig: queryJsConfig,
  getQueryString: GetQueryString,
  GetHashQueryString:GetHashQueryString,
  getUserInfo: (param) => {
    return new Promise((resolve, reject) => {
      api.get(checksign.apendSign('/wechat/getuserinfo'), param, r => {
        resolve(r)
      }, f => {
        reject(f)
      });
    });
  },
  getAccessToken: (param) => {
    return new Promise((resolve, reject) => {
      api.get(checksign.apendSign('/wechat/accesstoken'), param, r => {
        resolve(r)
      }, f => {
        reject(f)
      });
    });
  },
  getUserDetail: (param) => {
    return new Promise((resolve, reject) => {
      api.post(checksign.apendSign('/wechat/getuserdetail'), param, r => {
        resolve(r)
      }, f => {
        reject(f)
      });
    });
  },
  getBasePersonInfo:(param) => {
    return new Promise((resolve, reject) => {
      api.get(checksign.apendSign('/person/showInfo'), param, r => {
        resolve(r)
      }, f => {
        reject(f)
      });
    });
  },
  getPersonInfo: (param) => {
    return new Promise((resolve, reject) => {
      api.get(checksign.apendSign('/wechat/userinfo'), param, r => {
        resolve(r)
      }, f => {
        reject(f)
      });
    });
  },
  prefixInteger: (num, length) =>{
    return (Array(length).join('0') + num).slice(-length);
  }
}
